<?php
class Sb_Model_DbTable_Base
{
    protected $tableName;

    protected $_conn;
    
    public function __construct()
    {
    	$tableNameArr = explode('_', get_class($this));
    	$this->tableName = strtolower($tableNameArr[count($tableNameArr)-1]);
    	$this->_conn = Sb_Db::getInstance()->getConn();
    }
    
    public function featchAll()
    {
        $sql = 'select * from '.$this->tableName.' where active = 1';
        if (!($stmt = $this->_conn->prepare($sql))) {
        	echo "Prepare failed: (" . $this->_conn->errno . ") " . $this->_conn->error;
        }
        
        if (!$stmt->execute()) {
        	echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
        }
        
        if (!($res = $stmt->get_result())) {
        	echo "Getting result set failed: (" . $stmt->errno . ") " . $stmt->error;
        }
        $rows = array();
        while($row = $res->fetch_object()){
        	$rows[] = $row;
        }
        
        $stmt->close();
        return $rows;
    }
    
    public function delete($id)
    {
    	$id = (int)$id;
    	$sql = 'update '.$this->tableName.' set active = 0 where id = ?';
    	if (!($stmt = $this->_conn->prepare($sql))) {
    		echo "Prepare failed: (" . $this->_conn->errno . ") " . $this->_conn->error;
    	}
    	$stmt->bind_param('i', $id);
    	
    	if (!$stmt->execute()) {
    		echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
    	}
    	
    }
}